//
//  ButtonsCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/3/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class SettingsBtn: UIView {
    
    private lazy var icon: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.image = UIImage(named: "settings")
        return imgView
    }()
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel ()
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        lbl.textColor = .black
        lbl.sizeToFit()
        lbl.text = "Параметры"
        return lbl
    }()
    
    init () {
        super.init(frame: .zero)
        self.addView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    public func applyBlueStyle () {
        self.backgroundColor = .blueColor
        self.titleLbl.textColor = .white
        self.icon.setImageColor(color: .white)
        self.layer.cornerRadius = 15
    }
    
    
    private func addView () {
        self.layer.cornerRadius = 10
        self.clipsToBounds = true
        self.backgroundColor = .white
        self.addSubview(icon)
        self.addSubview(titleLbl)
        self.snp.makeConstraints { (make) in
            make.height.equalTo(48)
        }
        self.addConstraints()
    }
    private func addConstraints () {
        icon.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(32)
            make.width.height.equalTo(16)
            make.centerY.equalToSuperview()
        }
        titleLbl.snp.makeConstraints { (make) in
            make.leading.equalTo(icon.snp.trailing).offset(8)
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().inset(32)
        }
    }
}


final class ButtonsCell: UITableViewCell, ConfigurableCell, OptionCellProtocol {

    // MARK: - Outlets
    
    private lazy var scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.showsHorizontalScrollIndicator = false
        scroll.contentSize = CGSize(width: UIScreen.main.bounds.width, height: 48)
        return scroll
    }()
    
    private lazy var settingsBtn = SettingsBtn()
    
    // MARK: - Props
    
    var delegate: OptionCellActionProtocol!

    // MARK: - Actions
    
    @objc private func openFilter (sender: UITapGestureRecognizer) {
        delegate.openFilters()
    }
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(data object: Filter?, index: Int) {
        
    }
    
    func returnData() -> Filter? {
        return nil
    }
}

extension ButtonsCell {
    private func addViews () {
        self.contentView.backgroundColor = UIColor(red: 244/255, green: 246/255, blue: 250/255, alpha: 1)
        self.contentView.addSubview(scrollView)
        scrollView.addSubview(settingsBtn)
        self.addConstraints()
        
        let gesture = UITapGestureRecognizer(target: self, action: #selector(openFilter(sender: )))
        settingsBtn.addGestureRecognizer(gesture)
    }
    private func addConstraints () {
        self.scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        settingsBtn.snp.makeConstraints { (make) in
            make.height.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(16)
            make.width.equalTo(182)
        }
    }
}
